//
//  SWNewFeatureController.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/23.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit

class SWNewFeatureController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        view.backgroundColor = RandomColor()
        
        setupUI()
        
    }
    
    private func setupUI(){
        
        view.addSubview(collectionView)
        
        collectionView.registerClass(SWNewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")
        
        collectionView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
            
        }
        
        view.addSubview(startBtn)
        
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(-100)
            
        }
    }
    
    //懒加载collectionView
    
    private lazy var collectionView:UICollectionView = {
       
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight)
        
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        let collView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        collView.dataSource = self
        
        collView.delegate = self
        
        collView.pagingEnabled = true
        
        collView.showsHorizontalScrollIndicator = false
        
        collView.showsVerticalScrollIndicator = false
        
        
        return collView
        
    }()
    
    //懒加载按钮
    
    private lazy var startBtn:UIButton = {
        
       let btn = UIButton(textColor: UIColor.whiteColor(), fontSize: 18)
        
        btn.setTitle("开始体验", forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        btn.hidden = true
        
        btn.addTarget(self, action: "changeVCClick", forControlEvents: .TouchUpInside)
        
        return btn
        
    }()
    
    
    func changeVCClick(){
        
        NSNotificationCenter.defaultCenter().postNotificationName(WelcomeNoti, object: "newFeature")
        
    }
    
    
    

}


extension SWNewFeatureController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        
        let cell:SWNewFeatureCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! SWNewFeatureCollectionViewCell
        
        cell.imgIndex = indexPath.item
        
        cell.backgroundColor = RandomColor()
        
        return cell
        
    }
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        
        startBtn.hidden = true
    }
    
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int(collectionView.contentOffset.x / ScreenWidth)
        
        print(index)
        
        if index == 3{
            
            startBtn.hidden = false
        }else{
            startBtn.hidden = true
        }
//
//        UIView.animateWithDuration(0.25) { () -> Void in
//            
//            self.startBtn.hidden = false
//        }
        
//        let indexPath = NSIndexPath(forItem: index, inSection: 0)
//        
//        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! SWNewFeatureCollectionViewCell
//        
        
        
        
        
    }
    
    
    

    

    
}
